
#include "user_password.h"

#if GKT_USER_PASSWORD_DYNAMIC_ENABLE && USER_DYNPWD_SDED_ENABLE

int user_dynpwd_SDED_retrieve(user_dynpwd_data_s *data,
				uint32_t *values)
{
	uint32_t start_year_offset, expire_year_offset, tag;

	start_year_offset = USER_DYNPWD_SDED_FIELD(values[0], SD_YEAR_OFFSET);
	if (start_year_offset >= USER_DYNPWD_SDED_SD_YEAR_OFFSET_MAX) {
		gkt_trace("user_dynpwd_sded: start year_offset(%u) invalid!\n", 
				start_year_offset);
		return GKT_EPARAM;
	}

	data->sded.start_date.day = USER_DYNPWD_SDED_FIELD(values[0], SD_DAY);
	data->sded.start_date.month = USER_DYNPWD_SDED_FIELD(values[0], SD_MONTH);
	data->sded.start_date.year = start_year_offset + USER_DYNPWD_BASE_YEAR;
	if (!gkt_dt_check_date(&data->sded.start_date)) {
		gkt_trace("user_dynpwd_sded: start_date(%04u-%02u-%02u) invalid!\n", 
				data->sded.start_date.year, data->sded.start_date.month,
				data->sded.start_date.day);
		return GKT_EPARAM;
	}

	expire_year_offset = USER_DYNPWD_SDED_FIELD(values[0], ED_YEAR_OFFSET);
	if (expire_year_offset >= USER_DYNPWD_SDED_ED_YEAR_OFFSET_MAX) {
		gkt_trace("user_dynpwd_sded: expire year_offset(%u) invalid!\n", 
				expire_year_offset);
		return GKT_EPARAM;
	}

	data->sded.expire_date.day = USER_DYNPWD_SDED_FIELD(values[0], ED_DAY);
	data->sded.expire_date.month = USER_DYNPWD_SDED_FIELD(values[0], ED_MONTH);
	data->sded.expire_date.year = expire_year_offset + data->sded.start_date.year;
	if (!gkt_dt_check_date(&data->sded.expire_date)) {
		gkt_trace("user_dynpwd_sded: expire_date(%04u-%02u-%02u) invalid!\n", 
				data->sded.expire_date.year, data->sded.expire_date.month,
				data->sded.expire_date.day);
		return GKT_EPARAM;
	}
	else if (data->sded.expire_date.u32_value < data->sded.start_date.u32_value) {
		gkt_trace("user_dynpwd_sded: expire_date not later than start_date!\n");
		return GKT_EPARAM;
	}

	tag = USER_DYNPWD_SDED_FIELD(values[0], TAG);
	if (tag != USER_DYNPWD_SDED_TAG_VALUE) {
		gkt_trace("user_dynpwd_sded: tag(%u) invalid!\n", tag);
		return GKT_EPARAM;
	}

	return GKT_SUCCESS;
}

int user_dynpwd_SDED_verify(user_dynpwd_data_s *data,
				uint32_t user_index)
{
	const user_dynpwd_item_s *c_item;
	gkt_date_time_s date_time;
	uint32_t item_index;
	int retval;

	gkt_info("user_dynpwd_verify: <SDED> (%d) (%04u-%02u-%02u) (%04u-%02u-%02u)\n", 
		data->cancel,
		data->sded.start_date.year, data->sded.start_date.month,
		data->sded.start_date.day, data->sded.expire_date.year, 
		data->sded.expire_date.month, data->sded.expire_date.day);

	item_index = 0;
	while (item_index < g_user_dynpwd.instance.item_available_nums) {
		c_item = USER_DYNPWD_ITEM_C(item_index);
		if ((user_index == (uint32_t)c_item->ext_header.user_index)
			&& (USER_DYNPWD_TYPE_SDED == c_item->ext_header.dynpwd_type)
			&& (USER_ITEM_ST_CANCELLED == c_item->ext_header.state)
			&& (data->sded.start_date.u32_value == c_item->sded.start_date)
			&& (data->sded.expire_date.u32_value == c_item->sded.expire_date))
		{
			gkt_info("user_dynpwd_verify: <SDED> (%u) ALREADY CANCELLED\n", item_index);
			return GKT_USER_VERIFY_FAILED;
		}

		item_index++;
	}

	if (!data->cancel) {
		retval = gkt_rtc_get_time(&date_time);
		if (GKT_SUCCESS == retval) {
			if ((date_time.date.u32_value >= data->sded.start_date.u32_value)
				&& (date_time.date.u32_value <= data->sded.expire_date.u32_value))
			{
				return GKT_USER_VERIFY_PASSED;
			}
		}
	}
	else {
		gkt_trace("user_dynpwd_verify: <SDED> *** CANCEL ***\n");

		g_user_dynpwd.save_item.ext_header.user_index = user_index;
		g_user_dynpwd.save_item.ext_header.dynpwd_type = USER_DYNPWD_TYPE_SDED;
		g_user_dynpwd.save_item.sded.start_date = data->sded.start_date.u32_value;
		g_user_dynpwd.save_item.sded.expire_date = data->sded.expire_date.u32_value;
		user_dynpwd_save_item(&g_user_dynpwd.save_item, USER_ITEM_ST_CANCELLED);

		return GKT_USER_VERIFY_CANCELLED;
	}

	return GKT_USER_VERIFY_FAILED;
}

#endif

